package uoc.biblioteca.mappings.dao;

// Generated 11/04/2011 14:10:24 by Hibernate Tools 3.2.1.GA

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

import uoc.biblioteca.mappings.object.EstadoSolicitudVo;
import uoc.biblioteca.mappings.object.MilleniumVo;
import uoc.biblioteca.sessionFactory.HibernateSessionFactory;

/**
 * Home object for domain model class EstadoSolicitudVo.
 * @see uoc.biblioteca.mappings.dao.EstadoSolicitudVo
 * @author Hibernate Tools
 */
public class EstadoSolicitudHome extends BaseHome {

	private static final Log log = LogFactory.getLog(EstadoSolicitudHome.class);

	private final SessionFactory sessionFactory = null;


	public EstadoSolicitudVo findById(java.lang.Integer id) {
		log.debug("getting EstadoSolicitudVo instance with id: " + id);
		try {
			EstadoSolicitudVo instance = (EstadoSolicitudVo) sessionFactory
					.getCurrentSession().get(
							"uoc.biblioteca.mappings.object.EstadoSolicitudVo", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(EstadoSolicitudVo instance) {
		log.debug("finding EstadoSolicitudVo instance by example");
		try {
			List results = sessionFactory
					.getCurrentSession()
					.createCriteria(
							"uoc.biblioteca.mappings.object.EstadoSolicitudVo")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	
	//Mostrar todos los estados
	public List getAllEstatsCombo() {
		
		/* will hold the books we are going to return later */
		try{
			List estados = new ArrayList();
		
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();

			session.flush();
			
			tx = session.beginTransaction();
			
			//String hql= "select c from SolicitudLlibre as c where c.estadoSolicitud.idEstadoSolicitud != "+ID_ANULAT+" order by c.idSolicitud";
			String hql= "select c from uoc.biblioteca.mappings.object.EstadoSolicitudVo as c order by c.idEstadoSolicitud";
			
			List tmpEstados = session.createQuery(hql).list();
			
			for (Iterator iter = tmpEstados.iterator(); iter.hasNext();) {
				EstadoSolicitudVo estadoTemp = (EstadoSolicitudVo) iter.next();
				estadoTemp.getIdEstadoSolicitud();
				estadoTemp.getDescripcion();
				estados.add(estadoTemp);
			}
			

			tx.commit();
			
			return estados;
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//Mostrar todos los estados
	public List getAllEstatsComboGestion() {
		
		/* will hold the books we are going to return later */
		try{
			List estados = new ArrayList();
		
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();

			session.flush();
			
			tx = session.beginTransaction();
			
			//String hql= "select c from SolicitudLlibre as c where c.estadoSolicitud.idEstadoSolicitud != "+ID_ANULAT+" order by c.idSolicitud";
			String hql= "select c from uoc.biblioteca.mappings.object.EstadoSolicitudVo as c order by c.idEstadoSolicitud";
			
			List tmpEstados = session.createQuery(hql).list();
			
			for (Iterator iter = tmpEstados.iterator(); iter.hasNext();) {
				EstadoSolicitudVo estadoTemp = (EstadoSolicitudVo) iter.next();
				estadoTemp.getIdEstadoSolicitud();
				estadoTemp.getDescripcion();
				estados.add(estadoTemp);
			}
			

			tx.commit();
			
			return estados;
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//Mostrar todos los estados
	public EstadoSolicitudVo findByCodeMillenium(String codeMillenium) {
		
		/* will hold the books we are going to return later */
		try{
			EstadoSolicitudVo estado = new EstadoSolicitudVo();
		
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();

			session.flush();
			
			tx = session.beginTransaction();
			
			//String hql= "select c from SolicitudLlibre as c where c.estadoSolicitud.idEstadoSolicitud != "+ID_ANULAT+" order by c.idSolicitud";
			String hql= "select c from uoc.biblioteca.mappings.object.EstadoSolicitudVo as c where c.codeMillenium='"+codeMillenium+"' order by c.idEstadoSolicitud";
			
			List tmpEstados = session.createQuery(hql).list();
			
			for (Iterator iter = tmpEstados.iterator(); iter.hasNext();) {
				estado = (EstadoSolicitudVo) iter.next();
			}
			

			tx.commit();
			
			return estado;
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//Mostrar todos los estados
	public EstadoSolicitudVo[] getAllEstatsGestion() {
		
		/* will hold the books we are going to return later */
		try{
			List estados = new ArrayList();
		
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();

			session.flush();
			
			tx = session.beginTransaction();
			
			//String hql= "select c from SolicitudLlibre as c where c.estadoSolicitud.idEstadoSolicitud != "+ID_ANULAT+" order by c.idSolicitud";
			String hql= "select c from uoc.biblioteca.mappings.object.EstadoSolicitudVo as c where c.idEstadoSolicitud!=6 order by c.idEstadoSolicitud";
			
			List tmpEstados = session.createQuery(hql).list();
			
			for (Iterator iter = tmpEstados.iterator(); iter.hasNext();) {
				EstadoSolicitudVo estadoTemp = (EstadoSolicitudVo) iter.next();
				estadoTemp.getIdEstadoSolicitud();
				estadoTemp.getDescripcion();
				estados.add(estadoTemp);
			}
			

			tx.commit();
			
			return (EstadoSolicitudVo[]) estados.toArray(new EstadoSolicitudVo[0]);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	

	
}
